import { routes } from './../../main/store-product/store-product.routing';
import { Injectable, Inject, OnInit, Component } from "@angular/core";
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, NavigationStart } from "@angular/router";
import { DisplayService } from "./display.service";
import { LocalStorageService } from "../../lib/service/local-storage.service";
import { ActivatedRoute } from "@angular/router";

@Injectable()

export class DisplayGuard implements CanActivate {
  template_id;
  showcase_id;
  booth_number;
  constructor(
    private displayService: DisplayService,
    private localStorage: LocalStorageService,
    private router: Router,
    private routes: ActivatedRoute,
  ) {
    this.template_id = this.localStorage.getNowApplet()['template_id'];
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {

      var path = state;
      if (path.url == '/member/display') {
        this.displayService.getShowCaseList(this.template_id).subscribe(res => {
          if (res.code === 2000) {
            let list = res.data.data;
            this.showcase_id = list[0]['id'];
            this.booth_number = list[0]['booth_number'];
            this.router.navigate(['member/display/add/' + this.showcase_id + '/' + this.booth_number]);
            localStorage.setItem('addRouteUrl', `/add/${this.showcase_id}/${this.booth_number}`);
          }
        });
      };
    return true;
  }
}
